package cn.technology.forum.service.Impl;


import cn.technology.forum.dto.UserDTO;
import cn.technology.forum.mapper.UserAccountMapper;
import cn.technology.forum.mapper.UserMapper;
import cn.technology.forum.model.User;
import cn.technology.forum.model.UserAccount;
import cn.technology.forum.model.UserAccountExample;
import cn.technology.forum.model.UserExample;
import cn.technology.forum.service.UserManagerService;
import cn.technology.forum.service.UserService;
import cn.technology.forum.vo.PageVO;
import cn.technology.forum.vo.UserShow;
import cn.technology.forum.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class UserManagerServiceImpl implements UserManagerService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserService userService;

    @Override
    public PageVO<UserShow> list(UserVO userVO) {
        PageHelper.startPage(userVO.getPage(),userVO.getLimit());
        UserExample example = new UserExample();

        example.setOrderByClause("gmt_create desc");
        if(userVO.getId()!=null&&!"".equals(userVO.getId())){
            example.createCriteria().andIdEqualTo(userVO.getId());
        }
        if(userVO.getName()!=null&&!"".equalsIgnoreCase(userVO.getName())){
            example.createCriteria().andNameLike("%"+userVO.getName()+"%");
        }
        if(userVO.getAccountId()!=null&&!"".equalsIgnoreCase(userVO.getAccountId())){
            example.createCriteria().andAccountIdLike("%"+userVO.getAccountId()+"%");
        }
//        if(userVO.getRank()!=null){
//            example.createCriteria().andRankEqualTo(userVO.getRank());
//        }

        List<User> users = userMapper.selectByExample(example);
        List<UserShow> userShowList = users.stream().map(userService::getUserShow).collect(Collectors.toList());
        PageInfo<UserShow> pageInfo = new PageInfo<>(userShowList);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void setRank(UserAccount userVO) {
        User user = new User();
        System.out.println(userVO.toString());
        //BeanUtils.copyProperties(userVO,user);
        UserAccountExample  userAccountExample= new UserAccountExample();
        userAccountExample.createCriteria().andUserIdEqualTo(userVO.getId());
        userVO.setId(null);
        userAccountMapper.updateByExampleSelective(userVO,userAccountExample);
    }
}
